
var fs = require('fs');
var express = require('express');//引入express模块
var app = express();
var bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.text())


app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Content-Type,Access-Token");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1');
    res.header("Content-Type", 'application/x-www-form-urlencoded');
    next();
    });
let data= {code:0, msg: '提交成功！'}

//响应接口  上传robot协议
app.post('/upload/protocol', function(req,res){
    var txt = req.body.txt;
    if(req.body.txt){
        var json = JSON.stringify({
            msg:"提交成功",
            status: 200,
            code: 0
        });
        res.end(json, function(){
             // 保存到本地文件中
             fs.writeFile('robots.txt', txt, function(err) {
                if (err) {
                    console.log('出现错误!')
                }
                console.log('已输出至robots.txt中')
            })
        });
    }else{
        res.end(JSON.stringify({
            msg:"内容为空",
            status: 200,
            code: 1
        }));
    }
}).on('error', function(err) {
    console.log('错误信息：' + err)
})

//定义端口，此处所用为8081端口
var server = app.listen(8081,function(){
    console.log('runing 8081...');
})

